/**********************************************************/
/* Goblin Defense - A tower defense game for Wii Homebrew */
/* Copyright (C) 2009 Ricardo Bueno Cordeiro              */
/* Licensed under de GNU General Public License v2.0      */
/*   See license_gplv2.txt for detalis                    */
/**********************************************************/

#include <gccore.h>
#include <wiiuse/wpad.h>

#include "game.h"
#include "draw.h"
#include "random.h"

Game::Game() : map(this) {
	this->cursor_valid = 0;
	this->cursor_x = 0.0;
	this->cursor_y = 0.0;
	this->cursor_angle = 0.0;

	this->map.unload();
	this->toolbox.clear();
}

int Game::update(int state, u32 step, WPADData *wiimote_data) {
	if (state == CONTROL_HIDDEN)
		return 0;

	// Convert cursor postion to game position
	if ((this->cursor_valid = wiimote_data->ir.valid)) {
		this->cursor_x = DR_convert_cursor_x(wiimote_data->ir.x);
		this->cursor_y = DR_convert_cursor_y(wiimote_data->ir.y);
		this->cursor_angle = wiimote_data->ir.angle;
	}

	if ((wiimote_data->btns_d & WPAD_BUTTON_A) && (this->cursor_valid)) {
		if (this->map.inside(this->cursor_x, this->cursor_y))
			this->map.select_weapon(this->cursor_x, this->cursor_y);
		else if (this->toolbox.inside(this->cursor_x, this->cursor_y))
			this->toolbox.select_weapon(this->cursor_x, this->cursor_y);
		else if (this->map.get_selected_weapon() != NULL)
			this->map.get_selected_weapon()->click(this->cursor_x, this->cursor_y);
	}

	if ((wiimote_data->btns_d & WPAD_BUTTON_B) && (this->cursor_valid)) {
		if (this->map.inside(this->cursor_x, this->cursor_y) && this->toolbox.has_selected() && this->toolbox.can_buy() && this->toolbox.legal_position(&this->map, this->cursor_x, this->cursor_y)) {
			this->map.add_weapon(this->toolbox.buy_at(this->cursor_x, this->cursor_y));
		}
	}

	map.update(CONTROL_FOREGROUND, step, wiimote_data);
	
	return 0;
}

void Game::draw(int state) {
	if (state == CONTROL_HIDDEN)
		return;

	// Draw toolbox
	DR_configure(DRAW_CONF_DRAW_TEXTURE);
	this->toolbox.draw(CONTROL_FOREGROUND);	

	// Draw selected weapon info
	DR_configure(DRAW_CONF_DRAW_TEXTURE);
	UpgradableWeapon *w = this->map.get_selected_weapon();
	if (w != NULL)
		w->draw_info();

	// Draw map
	DR_configure(DRAW_CONF_DRAW_TEXTURE);
	this->map.draw(CONTROL_FOREGROUND);

	// Draw cursor
	if (this->cursor_valid) {
		DR_configure(DRAW_CONF_DRAW_TEXTURE);
		if (this->map.inside(this->cursor_x, this->cursor_y) && this->toolbox.has_selected())
			this->toolbox.draw_selected(this->cursor_x, this->cursor_y, this->toolbox.legal_position(&this->map, this->cursor_x, this->cursor_y));

		DR_configure(DRAW_CONF_DRAW_TEXTURE);
		Sprite::draw_sprite(this->cursor_x, this->cursor_y, 100.0, 100.0, DegToRad(-this->cursor_angle), sprite_cursor);
	}
}

void Game::text(int state) {
	if (state == CONTROL_HIDDEN)
		return;

	DR_draw_text(this->cursor_x, this->cursor_y, 40, "Teste");

	this->map.text(CONTROL_FOREGROUND);
	this->toolbox.text(CONTROL_FOREGROUND);
}

void Game::start_game(int map) {
	this->map.load(map);
	this->toolbox.clear();
}
